/**
 * Created by dale on 2017/10/19.
 */
var afficheList;
var startDom;
var endDom;
$(document).ready(function () {
    afficheList = $('#afficheList');
    $('.xh-datagrid-toolbar').parseToolbar();



    startDom    = $('#List_Search_StartTime');
    endDom      = $('#List_Search_EndTime');
    startDom.datebox();
    endDom.datebox();

    // startDom.datebox('setValue', GetDateStr(-7));
    // endDom.datebox('setValue', GetDateStr(0));

    afficheList.initDataGrid(baseUrl + 'frontend/msg/getAffiche',{queryParams:getParams()}).setSelectThenEnable(['bt_list_del']);

    $('#bt_list_add').click(function () {
        window.top.xhTop.showModal({
            title:'新增公告',
            content: '<iframe src="'+baseUrl+'frontend/msg/afficheForm"  style="width: 100%;;border-style: none;padding: 0px;height:500px"  scrolling="yes"   ></iframe>',
            button:'提交',
            backClose: false,
            width:970
        });
    });

    xhTool.bindDocKeyDown($('#bt_list_search'));
    $('#bt_list_search').click(function () {
        afficheList.addDataGridParams(getParams());
    });


    $('#bt_list_del').click(function () {
        if(xhExtra.buttonDisabled($(this))){
            return false;
        }
        var ids=afficheList.getDataGridSelectedFields('iNoId');
        myWindow.initPopUp({title: '确定要删除么?', text: '这些公告将被删除!'},function () {
            $.ajax({
                url:baseUrl+'frontend/msg/delAffiche',
                data:{ids:ids},
                type:'POST',
                success:function (data) {
                    window.top.swal(
                        data['msg'],
                        '',
                        'success'
                    );
                    afficheList.dataGridReload();
                },
                error:function (data) {
                    window.top.swal(
                        data['msg'],
                        '',
                        'error'
                    );
                }
            });
        });
    });

    $('.xh-datagrid').on('click','.record',function () {
        var iNoId=$(this).attr('id');
        window.top.xhTop.showModal({
            title:'查看进度',
            content: '<iframe src="'+baseUrl+'frontend/msg/noticeProcess?id='+iNoId+'"  style="width: 100%;;border-style: none;padding: 0px;height:520px"  scrolling="yes"   ></iframe>',
        });
    });



    DataGrid.onDblClickRow=function (rowIndex, rowData) {
        var iNoId = rowData['iNoId'];
        window.top.xhTop.showModal({
            title:'编辑公告',
            content: '<iframe src="'+baseUrl+'frontend/msg/afficheForm?iNoId='+iNoId+'"  style="width: 100%;;border-style: none;padding: 0px;height:500px"  scrolling="yes"   ></iframe>',
            button:'提交',
            backClose: false,
            width:970
        });
        return false;
    }
});
function GetDateStr(AddDayCount) {
    var dd = new Date();
    dd.setDate(dd.getDate()+AddDayCount);//获取AddDayCount天后的日期
    var y = dd.getFullYear();
    var m = dd.getMonth()+1;//获取当前月份的日期
    var d = dd.getDate();
    return y+"-"+m+"-"+d;
}

function getParams() {
    var key = xhExtra.getValue($('#list_search'));
    var startTime   = startDom.getElementValue(),
        endTime     = endDom.getElementValue();
    var type        =xhExtra.getValue($('#select_type'));
    var params={key:key,startTime:startTime,endTime:endTime,type:type};
    return params;
}